﻿using System;
using System.Windows.Forms;
using Arch.WinForm;
using Pharmacy.Data;
using Pharmacy.Entities;
using Pharmacy.Windows.Forms.App_Code;
using Pharmacy.Windows.Forms.App_Code.DSPrescriptionSlipTableAdapters;

namespace Pharmacy.Windows.Forms
{
    public partial class FormPrescriptionLabel : DevExpress.XtraEditors.XtraForm
    {
        protected TList<PrescriptionDetail> details;
        protected Prescription prescription;
        public FormPrescriptionLabel()
        {
            InitializeComponent();
            //LoadDefault();
            //btn_Refresh_Click(null, null);
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Control | Keys.P:
                    btn_Print_Click(null, null);
                    return true;
                case Keys.Escape:
                    if (MessageBox.Show("Close Form?", "Label Print Form", MessageBoxButtons.OKCancel) == DialogResult.OK)
                        this.Close();
                    return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
        public void LoadDefault(string prescriptionId)
        {
            this.Text = "Label - " + prescriptionId;
            prescription = DataRepository.PrescriptionProvider.GetByPrescriptionId(prescriptionId);
            lbl_DeliveryNote.Text = prescription.PrescriptionId;
            lbl_PatientCode.Text = prescription.PatientCode;
            lbl_PatientName.Text = prescription.PatientName;
            lbl_Address.Text = String.IsNullOrEmpty(prescription.Address) ? " " : prescription.Address;
            if (prescription.DateOfBirth != null)
                lbl_DateOfBirth.Text = prescription.DateOfBirth.Value.ToString("yyyy");
            else
                lbl_DateOfBirth.Text = " ";
            var dtDrugs = new DSPrescriptionSlip.DetailsDataTable();
            var adt = new DetailsADT { ClearBeforeFill = true };
            adt.Fill(dtDrugs, prescriptionId);
            details=new TList<PrescriptionDetail>();
            foreach (DSPrescriptionSlip.DetailsRow drug in dtDrugs)
            {
                details.Add(new PrescriptionDetail{ DrugId = drug.DrugId,DrugName = drug.DrugName,Unit = drug.Unit,Quantity = drug.Quantity});
            }
            //details = DataRepository.PrescriptionDetailProvider.GetByPrescriptionId(prescriptionId);
            gridControl1.DataSource = details;
        }

        private void btn_Print_Click(object sender, System.EventArgs e)
        {
            ApplicationSettings.LoadAppSettings();
            if (ApplicationSettings.LabelPrinter == null)
                MessageBox.Show("Please Set Printer First");
            else
            {
                ApplicationSettings.LoadAppSettings();
                foreach (var detail in details)
                {
                    if (detail.LabelQuantity > 0)
                    {
                        DataRepository.PrescriptionDetailProvider.DeepLoad(detail);
                        UpcLabel label = new UpcLabel();
                        label.DrugId = detail.DrugId;
                        label.BrandName = detail.DrugName;
                        DrugDispo drug = DataRepository.DrugDispoProvider.GetByDrugId(detail.DrugId);
                        label.GenericName = drug.GenericName;
                        label.UserGuide = drug.UseGuide;
                        label.Precaution = drug.Precaution;
                        label.PatientCode = prescription.PatientCode;
                        label.PatientName = prescription.PatientName;
                        label.Unit = detail.Unit;
                        int NumberOfLabel = detail.LabelQuantity;
                        label.Print(ApplicationSettings.LabelPrinter, NumberOfLabel);
                    }
                }
            }
        }
    }
}